//号码类
//邮政编码：6位 可从0开始
const zipCodeRegex = /^\d{6}$/
//社会信用码、工商注册号：18位阿拉伯数字或大写英文字母 无I O S V Z  
const codeNumberRegex = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/
//电话：座机
const telNumberRegex = /^(\d{3,4}-)?\d{7,8}$/
//手机号码
const mobileRegex = /^1[3456789]\d{9}$/
//传真
const faxRegex = /^(\d{3,4}-)?\d{7,8}$/
//资本、资金：无单位 小数
const numberRegex = /(^0$|^0\.[0-9]{0,4}$|^[1-9][0-9]{0,14}(\.[0-9]{0,4})?$)/
//编号(中文、英文、数字、下划线、连接线)
// const idNumberRegex = /^(\w*|[\u4E00-\u9FA5]*|-*)+$/
//编号(英文、数字、下划线、连接线)
const idNumberRegex = /^(\w*|-*)+$/
//整数（正数 或 负数 或 0）
const integerRegex = /^([1-9]\d*|-[1-9]\d*|0)$/
//正整数（不包含 0）
const positiveIntRegex = /^([1-9]\d*)$/
// IP的规则
const regIP = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
//证件
//身份证：18位 不考虑15位
const idCardRegex = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/
//军官证：例：粤字第XXXX号
const officerCardRegex = /^[\u4E00-\u9FA5]字第[0-9a-zA-Z]{4,8}号?$/
//护照
const passportRegex = /^([a-zA-z]|[0-9]){5,17}$/
//台湾居民身份证
const twIdCardRegex = /^[A-Z][0-9]{9}$/
//香港永久居民身份证
const hkIdCardRegex = /^[A-Za-z]\d{6}((\([0-9aA]\))|([0-9aA]))$/
//警官证
const policeCardRegex = /^\d{6,7}$/
//其他
//港澳居民来往内地通行证
const hmCardRegex = /^[HM]{1}\d{10}$/
//台湾居民来往大陆通行证
const twCardRegex = /^\d{8}$|^[0-9]{10}[A-Za-z]$/

//中文+英文+数字
//邮箱
const emailRegex = /^[A-Za-z\d]+[\w]*@([A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/
//中文
const chTxtRegex = /^[\u4e00-\u9fa5]+$/
//英文
const englishRegex = /^[A-Za-z]+$/
//包含中英文的普通文本（人的名称、职务等）：例：张三、Hannah Baker
const ordinaryTxtRegex = /^([\u2E80-\u9FFF]+|([A-Za-z]+(\s[A-Za-z]+)*))+$/
//包含中英文数字下划线 横杠
const projectNameRegex = /^([\u4e00-\u9fa5]*[0-9a-zA-Z_-]*)+$/
//包含中英文数字下划线 横杠的文本
const integrateTxtRegex = /^([\u4e00-\u9fa5]|[\w-])+$/
//日期：例：2021-01-01
const dateRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/
//年份
const yearRegex = /^\d{4}$/
//网址
const urlRegex = /^((http|https):\/\/)?[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?$/
//表单密码（英文、数字、符号）
const passwordRegex = /(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\w~!@#$%^&*?]{6,16}$/
//其他描述(中文、英文、数字、符号、空白字符)
const otherTxtRegex = /^(\w*\W*[\u4e00-\u9fa5]*\s*)+$/

function checkNull(val) {
  return val != undefined && val != null
}

function getRegex(min, max, regex, msg, extend) {
  let list = [{pattern:regex, message:msg, trigger:"blur"}]
  if(checkNull(min) || checkNull(max)) {
    list.push({
      validator: (rules, value, callback) => {
        if(checkNull(max) && value > max) {
          return callback(new Error("超出范围，请重新输入！"))
        } else if (checkNull(min) && value < min) {
          return callback(new Error("超出范围，请重新输入！"))
        } else {
          return callback()
        }
      },trigger: "blur"
    })
  }
  if (typeof extend === "object") {
    extend.forEach(e => {
      list.push(e)
    });
  }
  return list;
}

//是否必填
function addQuired(list, required, msg){
  if (typeof required == Boolean || required) {
    msg = msg == undefined ? '' : msg
    let obj = {required:true, message:msg, trigger:"blur"}
    list.push(obj)
  }
}

//验证邮编
function zipCode(required, msg, min, max, extend) {
  let list = getRegex(min, max, zipCodeRegex, "请重新输入 6 位邮政编码", extend)
  addQuired(list, required, msg)
  return list
}
//社会信用码、工商注册号
function codeNumber(required, msg, min, max, extend) {
  let list = getRegex(min, max, codeNumberRegex, "需输入18位号码（包含字母或数字）", extend)
  addQuired(list, required, msg)
  return list
}
//验证电话
function telNumber(required, msg, min, max, extend) {
  let list = getRegex(min, max, telNumberRegex, "请重新输入正确的电话号码，如0757-8888888", extend)
  addQuired(list, required, msg)
  return list
}
//手机号码
function mobile(required, msg, min, max, extend) {
  let list = getRegex(min, max, mobileRegex, "请重新输入正确的手机号码", extend)
  addQuired(list, required, msg)
  return list
}
//验证传真
function fax(required, msg, min, max, extend) {
  let list = getRegex(min, max, faxRegex, "请重新输入传真号码，如0757-8888888", extend)
  addQuired(list, required, msg)
  return list
}
//验证资本、资金
function number(required, msg, min, max, extend) {
  let list = getRegex(min, max, numberRegex, "请输入正确的金额（可包含15位整数和4位小数）", extend)
  addQuired(list, required, msg)
  return list
}
//编号（中文、英文、数字、下划线、连接线）
function idNumber(required, msg, min, max, extend) {
  let list = getRegex(min, max, idNumberRegex, "只能包含英文|数字|下划线|横杠", extend)
  addQuired(list, required, msg)
  return list
}
//整数
function integer(required, msg, min, max, extend) {
  let list = getRegex(min, max, integerRegex, "请重新输入整数", extend)
  addQuired(list, required, msg)
  return list
}
//正整数
function positiveInt(required, msg, min, max, extend) {
  let list = getRegex(min, max, positiveIntRegex, "请输入大于 0 的整数", extend)
  addQuired(list, required, msg)
  return list
}
//IP地址
function checkIP(required, msg, min, max, extend) {
  let list = getRegex(min, max, regIP, "请输入正确的IP格式", extend)
  addQuired(list, required, msg)
  return list
}
//身份证：18位 不考虑15位
function idCard(required, msg, min, max, extend) {
  let list = getRegex(min, max, idCardRegex, "请重新输入18位身份证号码", extend)
  addQuired(list, required, msg)
  return list
}
//护照
function passport(required, msg, min, max, extend) {
  let list = getRegex(min, max, passportRegex, "请重新输入护照号码（例：E12345678）", extend)
  addQuired(list, required, msg)
  return list
}
//军官证：例：粤字第XXXX号
function officerCard(required, msg, min, max, extend) {
  let list = getRegex(min, max, officerCardRegex, "军官证号码错误（例：粤字第XXXX号）", extend)
  addQuired(list, required, msg)
  return list
}
//台湾居民身份证
function twIdCard(required, msg, min, max, extend) {
  let list = getRegex(min, max, twIdCardRegex, "请重新输入10位台湾居民身份证号码", extend)
  addQuired(list, required, msg)
  return list
}
//香港永久居民身份证
function hkIdCard(required, msg, min, max, extend) {
  let list = getRegex(min, max, hkIdCardRegex, "请重新输入香港永久居民身份证号码", extend)
  addQuired(list, required, msg)
  return list
}
//警官证
function policeCard(required, msg, min, max, extend) {
  let list = getRegex(min, max, policeCardRegex, "请重新输入6位或7位警官证号码", extend)
  addQuired(list, required, msg)
  return list
}
//港澳居民来往内地通行证
function hmCard(required, msg, min, max, extend) {
  let list = getRegex(min, max, hmCardRegex, "请重新输入港澳居民来往内地通行证号码", extend)
  addQuired(list, required, msg)
  return list
}
//台湾居民来往大陆通行证
function twCard(required, msg, min, max, extend) {
  let list = getRegex(min, max, twCardRegex, "请重新输入台湾居民来往大陆通行证号码", extend)
  addQuired(list, required, msg)
  return list
}

//邮箱
function email(required, msg, min, max, extend) {
  let list = getRegex(min, max, emailRegex, "请重新输入正确的邮箱地址", extend)
  addQuired(list, required, msg)
  return list
}
//中文名称
function chTxt(required, msg, min, max, extend) {
  let list = getRegex(min, max, chTxtRegex, msg, extend)
  addQuired(list, required, msg)
  return list
}
//英文名称
function englishTxt(required, msg, min, max, extend) {
  let list = getRegex(min, max, englishRegex, msg, extend)
  addQuired(list, required, msg)
  return list
}
//包含中英文的普通名称（人、职务）：例：张三、Hannah Baker
function ordinaryTxt(required, msg, min, max, extend) {
  let list = getRegex(min, max, ordinaryTxtRegex, "只能包含中英文", extend)
  addQuired(list, required, msg)
  return list
}
//企业、项目名称
function projectName(required, msg, min, max, extend) {
  let list = getRegex(min, max, projectNameRegex, "请重新输入名称（包含中英文数字下划线 横杠）", extend)
  addQuired(list, required, msg)
  return list
}
//包含中英文数字下划线 横杠的文本
function integrateTxt(required, msg, min, max, extend) {
  let list = getRegex(min, max, integrateTxtRegex, "只能包含中英文|数字|下划线|横杠", extend)
  addQuired(list, required, msg)
  return list
}
//日期：例：2021-01-01
function date(required, msg, min, max, extend) {
  let list = getRegex(min, max, dateRegex, "请重新输入日期，如：2021-01-01", extend)
  addQuired(list, required, msg)
  return list
}
//年份：例：2021
function year(required, msg, min, max, extend) {
  let list = getRegex(min, max, yearRegex, "请重新输入年份，如：2021", extend)
  addQuired(list, required, msg)
  return list
}
//网址
function url(required, msg, min, max, extend) {
  let list = getRegex(min, max, urlRegex, "请重新输入正确的网址", extend)
  addQuired(list, required, msg)
  return list
}
//表单密码（英文、符号）
function password(required, msg, min, max, extend) {
  let list = getRegex(min, max, passwordRegex, "请重新输入6-16位密码（至少包含数字|英文|符号中的两种）", extend)
  addQuired(list, required, msg)
  return list
}
//其他描述(中文、英文、数字、符号、空白字符)
function otherTxt(required, msg, min, max, extend) {
  let list = getRegex(min, max, otherTxtRegex, msg, extend)
  addQuired(list, required, msg)
  return list
}

const Validate = {
  // /**
  //  * 校验密码
  //  * 密码必须由字母和数字组成，且不能少于6位
  //  */
  // validPassword(pwd) {
  //   const reg = /(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\w~!@#$%^&*?]{6,16}$/;
  //   return reg.test(pwd);
  // },

  zipCodeRegex:zipCodeRegex,
  codeNumberRegex:codeNumberRegex,
  telNumberRegex:telNumberRegex,
  mobileRegex:mobileRegex,
  faxRegex:faxRegex,
  numberRegex:numberRegex,
  idNumberRegex:idNumberRegex,
  integerRegex:integerRegex,
  positiveIntRegex:positiveIntRegex,
  regIP:regIP,

  idCardRegex:idCardRegex,
  passportRegex:passportRegex,
  officerCardRegex:officerCardRegex,
  twIdCardRegex:twIdCardRegex,
  hkIdCardRegex:hkIdCardRegex,
  policeCardRegex:policeCardRegex,
  hmCardRegex:hmCardRegex,
  twCardRegex:twCardRegex,

  emailRegex:emailRegex,
  chTxtRegex:chTxtRegex,
  englishRegex:englishRegex,
  ordinaryTxtRegex:ordinaryTxtRegex,
  projectNameRegex:projectNameRegex,
  integrateTxtRegex:integrateTxtRegex,
  dateRegex:dateRegex,
  yearRegex:dateRegex,
  urlRegex:urlRegex,
  passwordRegex:passwordRegex,
  otherTxtRegex:otherTxtRegex,

  zipCode: function (required, msg, min, max, extend){
    return zipCode(required, msg, min, max, extend)
  },
  codeNumber: function (required, msg, min, max, extend){
    return codeNumber(required, msg, min, max, extend)
  },
  telNumber: function (required, msg, min, max, extend){
    return telNumber(required, msg, min, max, extend)
  },
  mobile: function (required, msg, min, max, extend){
    return mobile(required, msg, min, max, extend)
  },
  fax: function (required, msg, min, max, extend){
    return fax(required, msg, min, max, extend)
  },
  number: function (required, msg, min, max, extend){
    return number(required, msg, min, max, extend)
  },
  idNumber: function (required, msg, min, max, extend){
    return idNumber(required, msg, min, max, extend)
  },
  integer: function (required, msg, min, max, extend){
    return integer(required, msg, min, max, extend)
  },
  positiveInt: function(required, msg, min, max, extend) {
    return positiveInt(required, msg, min, max, extend)
  },
  checkIP: function(required, msg, min, max, extend) {
    return checkIP(required, msg, min, max, extend)
  },
  validateIdCard(type, required, msg, min, max, extend){
    switch(type) {
      case 1 :
        return idCard(required, msg, min, max, extend)
      case 2 :
        return officerCard(required, msg, min, max, extend);
      case 3 :
        return passport(required, msg, min, max, extend);
      case 4 :
        return twIdCard(required, msg, min, max, extend);
      case 5 :
        return hkIdCard(required, msg, min, max, extend);
      case 6 :
        return policeCard(required, msg, min, max, extend);
      case 99:
        return idNumber(required, msg, min, max, extend);
      default:
        break;
    }
  },

  email: function (required, msg, min, max, extend){
    return email(required, msg, min, max, extend)
  },
  chTxt: function (required, msg, min, max, extend){
    return chTxt(required, msg, min, max, extend)
  },
  englishTxt: function (required, msg, min, max, extend){
    return englishTxt(required, msg, min, max, extend)
  },
  ordinaryTxt: function (required, msg, min, max, extend){
    return ordinaryTxt(required, msg, min, max, extend)
  },
  projectName: function (required, msg, min, max, extend){
    return projectName(required, msg, min, max, extend)
  },
  integrateTxt: function (required, msg, min, max, extend){
    return integrateTxt(required, msg, min, max, extend)
  },
  date: function (required, msg, min, max, extend){
    return date(required, msg, min, max, extend)
  },
  year: function (required, msg, min, max, extend){
    return year(required, msg, min, max, extend)
  },
  url: function (required, msg, min, max, extend){
    return url(required, msg, min, max, extend)
  },
  password: function (required, msg, min, max, extend){
    return password(required, msg, min, max, extend)
  },
  otherTxt: function (required, msg, min, max, extend){
    return otherTxt(required, msg, min, max, extend)
  },
}

export default Validate;
